package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.dto.RepairAliYunCallErrorNumberDTO;
import com.xbongbong.paas.pojo.dto.RepairAliYunCallErrorNumberDataDTO;
import com.xbongbong.paas.pojo.vo.RepairAliYunCallErrorNumberVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.entity.CallCenterCallLogEntity;
import com.xbongbong.saas.model.CallCenterCallLogModel;
import com.xbongbong.saas.service.RepairAliYunCallService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author WuHB
 * @date 2022/09/01 16:01
 */
@Service("repairAliYunCallService")
public class RepairAliYunCallServiceImpl implements RepairAliYunCallService {

    @Resource
    private CallCenterCallLogModel callCenterCallLogModel;


    /**
     * 获取错误的阿里云呼通话记录信息
     *
     * @param recordDto 查询条件
     * @return 错误的阿里云呼通话记录信息
     * @throws XbbException 异常
     */
    @Override
    public RepairAliYunCallErrorNumberVO repairAliYunCallErrorNumber(RepairAliYunCallErrorNumberDTO recordDto) throws XbbException {
        List<RepairAliYunCallErrorNumberDataDTO> data = recordDto.getData();
        if (CollectionsUtil.isEmpty(data)) {
            return new RepairAliYunCallErrorNumberVO();
        }
        List<RepairAliYunCallErrorNumberDataDTO> dataList = recordDto.getData();
        List<String> callIdList = dataList.stream().map(RepairAliYunCallErrorNumberDataDTO::getCallId).collect(Collectors.toList());
        // 根据通话ID查询通话记录
        Map<String, Object> param = new HashMap<>(3);
        param.put("corpid", recordDto.getCorpid());
        param.put("callIdIn", callIdList);
        param.put("del", DelEnum.NORMAL.getDel());
        List<CallCenterCallLogEntity> centerCallLogEntityList = callCenterCallLogModel.findEntitys(param);
        if (CollectionsUtil.isEmpty(centerCallLogEntityList)) {
            return new RepairAliYunCallErrorNumberVO();
        }
        Map<String, RepairAliYunCallErrorNumberDataDTO> callIdMap = dataList.stream().collect(Collectors.toMap(RepairAliYunCallErrorNumberDataDTO::getCallId, dto -> dto, (oldValue, newValue) -> newValue));
        for (CallCenterCallLogEntity entity : centerCallLogEntityList) {
            String callId = entity.getCallId();
            RepairAliYunCallErrorNumberDataDTO dataDto = callIdMap.getOrDefault(callId, null);
            if (Objects.isNull(dataDto)) {
                continue;
            }
            entity.setAgentPhoneNum(dataDto.getAgentPhoneNum());
            entity.setAnotherPhoneNum(dataDto.getAnotherPhoneNum());
        }
        callCenterCallLogModel.batchUpdateAliyunCallCenter(recordDto.getCorpid(), centerCallLogEntityList);
        return new RepairAliYunCallErrorNumberVO();
    }
}
